 /*
  *@(#)LocalRequest.java		13 Sept. 2000
  *
  * Copyright: MetaPlus Inc & Inquiry Management Systems Ltd.  2000
  *
  * This software is the proprietary information of MetaPlus Inc & 
  * Inquiry Management Systems Ltd.  
  * Use is subject to license terms.
  */
package com.ims.iaa;


import java.io.*; 
import java.sql.Timestamp;

/**
 * Data model of AdRequest which can be stored into database table <code>
 * tblSearch</code>, <code>tblRequest</code> and <code>tblRequestParams</code>.
 * Provides the actual services for the AdRequest.
 * This class can be serialized and sent to a different JVM and it is used as a 
 * delegatee of the remote wrapper class <code>AdTrackRequestImpl</code>.
 * 
 * @author: Anthony Zhang
 *
 * @see AdTrackRequestImpl
 *
 * @version 1.0 25 Sept 2000
 */
public class LocalRequest implements Serializable, Cloneable {
	/**
	  * Lock object provide synchronization for update 
	  * information in the databse. Alloe only one method to access the databse 
	  * at the same time.
	  */
	static private Object lock = new Object();

	/**
	  * Represent the primary key of the table </code>tblSearch</code>. It is 
	  * retrieved when the search entry is stored into the table.
	  *
	  * @see #getSearchId
	  * @see #setSearchId	  
	  * @see SpiderDBConnectionImpl#getSearchIdFromDB
	  * @see SpiderDBConnection#getSearchIdFromDB
	  *
	  */
	private int searchId = -1; 
	private Timestamp processStartTime;
	private Timestamp processEndTime;
	private String spiderName = "";
	private int searchState = 0;

	private int requestId = -1;
	private int customerId = 1; 
	private String siteURL;
	private int searchDepth;
	private int maxRunningTime = 30;
	private String samplingMethod = "once";
	private RequestParameters reqParameters = null;
	//add by ShengMengBai at 2005-04-29
	private UserAgentAux m_oUserAgent = null;
	private boolean m_bObeyRobotsTXT = false;
	//private String userAgent = "Mozilla/";
	//++Daniel B.  Jan/15/07 - for max impressions
	private int maxImpressions = Integer.MAX_VALUE;
	
	private String startParams;
	private String endParams;

	//++Daniel B.  Jan/15/07 - for max impressions
	public int getMaxImpressions() {
		return maxImpressions;
	}
	
	//++Daniel B.  Jan/15/07 - for max impressions
	public void setMaxImpressions(int max) {
		maxImpressions = max;
	}

	public String getStartParams()  {
		return startParams;
	}
	public void setStartParams(String params)  {
		startParams = params;
	}

	public String getEndParams()  {
		return endParams;
	}
	public void setEndParams(String params)  {
		endParams = params;
	}

	private SpiderDBConnection  sp;

	public void setRequestParams(RequestParameters reqParams)  {
		reqParameters = reqParams;
	}
	public RequestParameters getRequestParams()  {
		return reqParameters;
	}
	//++ add by ShengMengBai at 2005-05-29
	public void setUserAgentObject(UserAgentAux obj){
	    m_oUserAgent = obj;
	}
	public boolean getObeyRobotsTXT(){
	    return m_bObeyRobotsTXT;
	}
	public void setObeyRobotsTXT(boolean bValue){
	    m_bObeyRobotsTXT = bValue;
	}
	public UserAgentAux getUserAgentObject(){
	    return m_oUserAgent;
	}
	//-- add by ShengMengBai at 2005-05-29
/*	delete by ShengMengBai at 2005-05-29
	public void setUserAgent(String agent)  {
		userAgent = agent;
	}
	public String getUserAgent()  {
		return userAgent;
	}
*/
	public Object clone()  {
		try  {
			 return super.clone();
		} catch (CloneNotSupportedException e)  {
			IaaUtil.log("Clone not supported: " + e);
		}
		return null;
	}
	protected void setSpiderDB(SpiderDBConnection spCon)  {
		sp = spCon;
	}
	public SpiderDBConnection getSpiderDB()  {
		return sp;
	}
	public int getCustomerId ()  {
		return customerId;
	}
	public void setCustomerId (int companyId)  {
		customerId = companyId;
	}
	public int getRequestId()  {
		return requestId;
	}
	public void setRequestId(int id)  {
		requestId = id;
	}
	public int getSearchId()  {
		return searchId;
	}
	public void setSearchId(int id)  {
		searchId = id;
	}
	public void setMaxRunningTime(int time)  {
		maxRunningTime = time;
	}
	public int getMaxRunningTime()  {
		return maxRunningTime;
	}
	public String getSamplingMethod()  {
		return samplingMethod;
	}
	public void setSamplingMethod(String method)  {
		samplingMethod = method;
	}
	public void setSpiderName(String name)  {
		spiderName = name;
		if(IaaDebug.StoreDB)
		 synchronized(lock) 
		 {
			 updateSpiderName();		
		 }
	}
	public String getSpiderName()  {
		return spiderName;
	}
	public void setSearchState(int state)  {
		searchState = state;
		if (IaaDebug.StoreDB)  
			synchronized(lock) 
			{
				updateState();
			}
	}
	public int getSearchState()  {
		return searchState;
	}
	public LocalRequest() {
	
	}
	
	public LocalRequest(String URL,int depth) {
		siteURL = URL;
		searchDepth = depth;
	}
	
/*	public LocalRequest(LocalRequest lr)  {
		this(lr.getUrl(), lr.getDepth(),lr.getCustomerId(),
			lr.getMaxRunningTime(),lr.getSamplingMethod());
		processStartTime = lr.getStartTime();
		processEndTime =  lr.getEndTime();
		requestId = lr.getRequestId();
		searchId = lr.getSearchId();
		spiderName = lr.getSpiderName();
		searchState = lr.getSearchState();
		sp = lr.getSpiderDB();
	} */
	
	public boolean equals(LocalRequest adReq) {
		return ( (siteURL.equals(adReq.getUrl()) && 
					(searchDepth == adReq.getDepth()) &&
					(maxRunningTime == adReq.getMaxRunningTime()) &&
					(samplingMethod == adReq.getSamplingMethod())  )?
			true: 
			false );
	}
	
	public void setUrl(String URL) {
		siteURL = URL;
	}
	
	public String getUrl() {
		return siteURL;	
	}
	
	public void setDepth(int depth) {
		searchDepth = depth;
	}
	
	public int getDepth() {
		return searchDepth;	
	}
	
	public void setStartTime(Timestamp startTime) {
		processStartTime = startTime;
		if(IaaDebug.StoreDB)
		 synchronized(lock) 
		 {
			 updateStartTime();
		 }
	}
	
	public Timestamp getStartTime() {
		return processStartTime;
	}

	public void setEndTime(Timestamp endTime) {
		processEndTime = endTime;
//++ 2007-5-9 deleted by changsheng,will updated in updateState()		
		/*if(IaaDebug.StoreDB) 
		synchronized(lock) 
		{
		  updateEndTime();
		}*/
//--
	}
	
	public Timestamp getEndTime() {
		 return processEndTime;
	}
	
/*	public int getSearchIdFromDB() throws SQLException {
		return sp.getSearchIdFromDB(this);
	}

	public int getRequestIdFromDB()  throws SQLException{
		return sp.getRequestIdFromDB(this);
	}
*/				
	protected void updateStartTime() {
		try  {
			sp.updateRequestStartTime(searchId, processStartTime, IaaThread.getLogString());
		} catch ( Exception e)  {
			IaaUtil.log("updateRequestStartTime Exception:"+e);
		}	
	}

	protected void updateEndTime() {
		  if(IaaDebug.processDebug)
		  {
		  int count=TestUtil.count++;
		  String tName=Thread.currentThread().getName();
		  //Method A call Method B
		  StackTraceElement[] st=new Exception().getStackTrace();
		  StackTraceElement aA=st[1];
		  StackTraceElement aB=st[0];
		  String say=aA.getClassName()+":"+aA.getMethodName()+":"+aA.getLineNumber()+
		  " call "+aB.getClassName()+":"+aB.getMethodName()+aB.getLineNumber();
		  IaaUtil.log("wukun:"+count+":"+tName+":"+say);
		  }
		try  {
			sp.updateRequestEndTime(searchId, processEndTime, IaaThread.getLogString());
		} catch ( Exception e)  {
			IaaUtil.log("updateRequestEndTime Exception:"+e);
		}	
	}

	protected void updateSpiderName() {
	      if(IaaDebug.processDebug)
	      {
	      int count=TestUtil.count++;
	      String tName=Thread.currentThread().getName();
	      //Method A call Method B
	      StackTraceElement[] st=new Exception().getStackTrace();
	      StackTraceElement aA=st[1];
	      StackTraceElement aB=st[0];
	      String say=aA.getClassName()+":"+aA.getMethodName()+":"+aA.getLineNumber()+
	      " call "+aB.getClassName()+":"+aB.getMethodName()+aB.getLineNumber();
	      IaaUtil.log("wukun:"+count+":"+tName+":"+say);
	      }
		
		try  {
			sp.updateRequestSpiderName(searchId,spiderName);
		} catch ( Exception e)  {
			IaaUtil.log("updateRequestSpiderName Exception:"+e);
		}	
	}

	protected void updateState() {
		
	      if(IaaDebug.processDebug)
	      {
	      int count=TestUtil.count++;
	      String tName=Thread.currentThread().getName();
	      //Method A call Method B
	      StackTraceElement[] st=new Exception().getStackTrace();
	      StackTraceElement aA=st[1];
	      StackTraceElement aB=st[0];
	      String say=aA.getClassName()+":"+aA.getMethodName()+":"+aA.getLineNumber()+
	      " call "+aB.getClassName()+":"+aB.getMethodName()+aB.getLineNumber();
	      IaaUtil.log("wukun:"+count+":"+tName+":"+say);
	      }
		
		try  {
			sp.updateRequestState(searchId, searchState, IaaThread.getLogString());
		} catch ( Exception e)  {
			IaaUtil.log("updateRequestState Exception:"+e);
		}	
	}
	public String toString()  {
		String str = "LocalRequest: " + getRequestId() + " url: " + getUrl() 
							+ " Depth: " +getDepth() + " ID" + getSearchId();
		return str;
	}
	//test purpose
	public static void main(String [] args)  {
		LocalRequest lr = new LocalRequest("http://www.netease.com",2);
		lr.storeAdRequest();
	}
	
	protected void storeAdRequest()  {
		
	      if(IaaDebug.processDebug)
	      {
	      int count=TestUtil.count++;
	      String tName=Thread.currentThread().getName();
	      //Method A call Method B
	      StackTraceElement[] st=new Exception().getStackTrace();
	      StackTraceElement aA=st[1];
	      StackTraceElement aB=st[0];
	      String say=aA.getClassName()+":"+aA.getMethodName()+":"+aA.getLineNumber()+
	      " call "+aB.getClassName()+":"+aB.getMethodName()+aB.getLineNumber();
	      IaaUtil.log("wukun:"+count+":"+tName+":"+say);
	      }
		
		if (IaaDebug.Debug) IaaUtil.log("Begin store AdRequest");		
		try  {
			if ( requestId == -1)  {
				setRequestId(sp.getRequestIdFromDB(this, IaaThread.getLogString()));
			}
			setSearchId(sp.getSearchIdFromDB(this, IaaThread.getLogString()));
		} catch (Exception e)  {
			IaaUtil.log("Store ad request error " + e);	
		}
		if (IaaDebug.Debug) IaaUtil.log("End store AdRequest");		
	}

	private int specialJS = 0 ;
	public void setSpecialJS(int specialJS)
	{
		this.specialJS = specialJS ;
	}
	public int getSpecialJS()
	{
		return this.specialJS ;
	}
}